package com.example.administrator.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class EditActivity extends AppCompatActivity {
    private TextView txtNo;
    private EditText edtName;
    private EditText edtAge;
    private Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        findViews();

        initData();

        initBtnSave();
    }

    private void initBtnSave() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String no = txtNo.getText().toString();
                String name = edtName.getText().toString();
                int age = Integer.parseInt(edtAge.getText().toString());

                Student student = new Student(no, name, age);

                Intent intent = new Intent();
                intent.putExtra("STUDENT", student);
                setResult(3001, intent);
                finish();
            }
        });
    }

    private void initData() {
        Student student = (Student) getIntent().getSerializableExtra("STUDENT");
        txtNo.setText(student.getNo());
        edtName.setText(student.getName());
        edtAge.setText(String.valueOf(student.getAge()));
    }

    private void findViews() {
        txtNo = findViewById(R.id.txt_no);
        edtName = findViewById(R.id.edt_name);
        edtAge = findViewById(R.id.edt_age);
        btnSave = findViewById(R.id.btn_save);
    }
}
